﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Pipes.Queries.Model.Pattern
{
  public class PatternOpts : Query.IExpression, Parser.IPatternVisitable
  {
    private List<Pattern> _patterns = new List<Pattern>();
    private RegexOptions _opts;

    public bool Deterministic { get { return true; } }
    public Pipes.Queries.MetaData.GeneralDataType InferredType { get; set; }
    public RegexOptions Options
    {
      get
      {
        return _opts;
      }
      set
      {
        _opts = value;
      }
    }
    public IList<Pattern> Patterns
    {
      get
      {
        return _patterns;
      }
    }

    public override string ToString()
    {
      var builder = new StringBuilder("'");
      for (var i = 0; i < _patterns.Count; i++)
      {
        if (i > 0) builder.Append("|");
        builder.Append(_patterns[i]).ToString();
      }
      builder.Append("'");
      return builder.ToString();
    }

    public void Visit(Parser.IQueryVisitor visitor)
    {
      visitor.Visit(this);
    }
    public void Visit(Parser.IPatternVisitor visitor)
    {
      visitor.Visit(this);
    }
    public object Evaluate()
    {
      var writer = new Provider.RegExpWriter();
      Visit(writer);
      return writer.ToString();
    }
  }
}
